package org.example.comp_algorithm_back.util;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataCleanUtil {
    public static void Clean(String wordIn, String wordOut) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        File fileOut = new File(wordOut);
        InputStreamReader inStream = new InputStreamReader(new FileInputStream(wordIn), "GBK");//读取文件
        OutputStreamWriter outStream = new OutputStreamWriter(new FileOutputStream(wordOut), "utf-8");
        BufferedReader bf = new BufferedReader(inStream);
        BufferedWriter bw = new BufferedWriter(outStream);
        String valueString = null;
        char[] c;
        while ((valueString=bf.readLine())!=null){
            c = valueString.toCharArray();
            for(int i = 39; i < valueString.length(); i++) { //只保留下关键字
                bw.append(c[i]);
                if(c[i] == '\t') {
                    bw.newLine();
                }
            }
            if(fileOut.length() > 10000000) break;//取10M的数据10*1024*1024
        }
        bw.close();
    }

    public static void surveyClean(String inputFilePath, String outputFilePath, int wordCount) {
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length == wordCount) { // 确保每行有对应个数的个评分
                    boolean isValid = true;
                    List<Integer> scores = new ArrayList<>();
                    for (String part : parts) {
                        int score;
                        try {
                            score = Integer.parseInt(part);
                            if (score < 1 || score > 5) {
                                isValid = false; // 评分不在1-5之间
                                break;
                            }
                            scores.add(score);
                        } catch (NumberFormatException e) {
                            isValid = false; // 无法转换为整数
                            break;
                        }
                    }

                    if (isValid && !isHighlyRepetitive(scores)) {
                        writer.write(line);
                        writer.newLine();
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查评分是否高度重复
     * @param scores 评分列表
     * @return 如果评分高度重复返回true，否则返回false
     */
    private static boolean isHighlyRepetitive(List<Integer> scores) {
        Map<Integer, Integer> scoreCount = new HashMap<>();
        for (int score : scores) {
            scoreCount.put(score, scoreCount.getOrDefault(score, 0) + 1);
        }

        int maxCount = 0;
        for (int count : scoreCount.values()) {
            if (count > maxCount) {
                maxCount = count;
            }
        }

        // 如果某个评分出现次数超过总评分数量的一半，则认为是高度重复
        return maxCount > (scores.size() / 2);
    }

    public static void Check(){
        if (!new File(PathClass.wordOut).exists()) {
            try {
                DataCleanUtil.Clean(PathClass.file2,PathClass.wordOut);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


